package com.media.showimglist;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.ObjectKey;
import com.media.R;
import com.xinlan.imageeditlibrary.editimage.utils.BaseUtils;
import com.xinlan.imageeditlibrary.editimage.view.imagezoom.ImageViewTouch;
import com.xinlan.imageeditlibrary.editimage.view.imagezoom.ImageViewTouchBase;

import java.io.File;
import java.util.Objects;

public class ShowImgListFragment extends Fragment {
    private ImageViewTouch mIvEditimg;
    private View mInflate;
    private String mImgUrl = "";

    private static final String TAG = "ShowImgListFragment";
    protected boolean isVisible;
    private boolean isPrepared;
    private boolean isFirst = true;
    private ProgressBar mProgressBar;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        lazyLoad();
        //加载数据
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mInflate = inflater.inflate(R.layout.fragment_show_img_list, container, false);

        mIvEditimg = mInflate.findViewById(R.id.iv_editimg);
        mProgressBar = mInflate.findViewById(R.id.ProgressBar);

        if (getArguments() != null) {
            mImgUrl = getArguments().getString("url");
        }
        return mInflate;
    }

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirst) {
            return;
        }
        try {
            mProgressBar.setVisibility(View.VISIBLE);
            loadImg(mImgUrl);
        } catch (Exception e) {
            e.getMessage();
        }
        isFirst = false;
    }


    public void setNewImgUrl(String newImgUrl) {
        if (!TextUtils.isEmpty(newImgUrl)) {
            loadImg(newImgUrl);
        }
    }

    private void loadImg(final String newImgUrl) {


        final RequestOptions userAvatarOptions = new RequestOptions()
                .signature(new ObjectKey(System.currentTimeMillis()));
        Glide.with(requireContext().getApplicationContext())
                .applyDefaultRequestOptions(userAvatarOptions)
                .asBitmap().load(newImgUrl)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        setIngBitmap(bitmap, newImgUrl, userAvatarOptions);
                    }
                });
    }

    private void setIngBitmap(@NonNull Bitmap bitmap, String newImgUrl, RequestOptions userAvatarOptions) {
        int mBitHeight = bitmap.getHeight();
        int mScreenHeight = BaseUtils.getScreenHeight(requireContext());

        if (mBitHeight >= mScreenHeight * 5) {
            mIvEditimg.setLongGraph(true);
        } else {
            mIvEditimg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        }

        Glide.with(getContext())
                .applyDefaultRequestOptions(userAvatarOptions)
                .load(newImgUrl)
                .placeholder(R.drawable.ic_def_hanxiang_228)
                .error(R.drawable.ic_def_hanxiang_228)
                .into(mIvEditimg);
        mProgressBar.setVisibility(View.GONE);
    }

    public static Bitmap getDiskBitmap(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }


}
